<?php

class BizcardMail
{

    /**
     *
     * @var instance
     * @access private
     */
    private static $_instance;

    private $_mail = null;

    public function __construct ()
    {
        $config = new Zend_Config_Ini(APPLICATION_PATH . "/configs/application.ini", APPLICATION_ENV);
        
        $conf = array(
                'auth' => $config->mail->auth,
                'username' => $config->mail->user,
                'password' => $config->mail->password
        );
        
        if (isset($config->mail->ssl)) {
            $conf['ssl'] = $config->mail->ssl;
        }
        
        if (isset($config->mail->port)) {
            $conf['port'] = $config->mail->port;
        }
        
        $this->_mail = new Zend_Mail('UTF-8');
        $this->_mail->setDefaultTransport(new Zend_Mail_Transport_Smtp($config->mail->smtp, $conf));
        $this->_mail->setDefaultFrom($config->mail->default->from, $config->mail->default->fromName);
    }

    /**
     *
     * @var instance
     * @access public
     * @return IforetMail
     */
    public static function getInstance ()
    {
        if (! isset(self::$_instance)) {
            self::$_instance = new BizcardMail();
        }
        return self::$_instance;
    }

    /**
     *
     * @param String $subject            
     * @param String $body            
     * @param mixed $to            
     * @param String $from            
     * @param mixed $cc            
     * @param mixed $bcc            
     * @param string $type            
     * @return bool
     */
    public static function sendMail ($subject, $body, $to, $attachment = array(), $from = null, $fromName = null, $cc = array(), $bcc = array(), $type = 'text')
    {
    	$mail = self::getInstance()->_mail;
        // Subject
       
        $mail->clearSubject();
        $mail->setSubject($subject);
        
        // Body
        if ($type == 'text') {
            $mail->setBodyText($body);
        } else {
            $mail->setBodyHtml($body);
        }
        
        // To
        if (! is_array($to)) {
            $to = array(
                    $to
            );
        }
        foreach ($to as $address) {
        	$mail->clearRecipients();
            $mail->addTo($address);
        }
        
        // From
        if (! is_null($from) && ! empty($from)) {
        	
            $mail->setFrom($from, $fromName);
        }
        
        // CC
        if (! is_array($cc)) {
            $cc = array(
                    $cc
            );
        }
        foreach ($cc as $address) {
            $mail->addCc($address);
        }
        if (! is_array($bcc)) {
            $bcc = array(
                    $bcc
            );
        }
        foreach ($bcc as $address) {
            $mail->addBcc($address);
        }
        
        foreach ($attachment as $filename => $filepath) {
            if ($filename) {
                $attach = new Zend_Mime_Part(@file_get_contents($filepath));
                $attach->type = 'application/pdf';
                $attach->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
                $attach->encoding = Zend_Mime::ENCODING_BASE64;
                $attach->filename = $filename;
                $mail->addAttachment($attach);
            }
        }
        
        try {
            $mail->send();
        } catch (Exception $e) {
            Zend_Registry::get("Kzend_Logger")->err('Can Not Send Email >> ' . $e);
            return false;
        }
        Zend_Registry::get("Kzend_Logger")->info('Can Not Send Email ');
        return true;
    }

    /**
     *
     * @param String $subject            
     * @param String $body            
     * @param mixed $to            
     * @param String $from            
     * @param String $fromName            
     * @param mixed $cc            
     * @param mixed $bcc            
     * @return bool
     */
    public static function sendTextMail ($subject, $body, $to, $from = null, $fromName = null, $cc = array(), $bcc = array())
    {
        return self::sendMail($subject, $body, $to, $from, $fromName, $cc, $bcc, 'text');
    }

    /**
     *
     * @param String $subject            
     * @param String $body            
     * @param mixed $to            
     * @param String $from            
     * @param String $fromName            
     * @param mixed $cc            
     * @param mixed $bcc            
     * @return bool
     */
    public static function sendHtmlMail ($subject, $body, $to, $attachment = array(), $from = null, $fromName = null, $cc = array(), $bcc = array())
    {
        return self::sendMail($subject, $body, $to, $attachment, $from, $fromName, $cc, $bcc, 'html');
    }
    
    /**
     *
     * @param String $subject            
     * @param String $body            
     * @param mixed $to            
     * @param String $from            
     * @param String $fromName            
     * @param string $type            
     * @return bool
     */
    public function sendNotificationMail ($subject, $body, $to, $from = null, $fromName = null, $type = 'html')
    {
        $mail = $this->_mail;
        
        // Subject
        $mail->setSubject($subject);
        
        // Body
        if ($type == 'text') {
            $mail->setBodyText($body);
        } else {
            $mail->setBodyHtml($body);
        }
        
        // To
        if (! is_array($to)) {
            $to = array(
                    $to
            );
        }
        foreach ($to as $address) {
            $mail->addTo($address);
        }
        
        try {
            $mail->send();
        } catch (Exception $e) {
            Zend_Registry::get("Kzend_Logger")->err('Can Not Send Notification Email >> ' . $e);
            return false;
        }
        Zend_Registry::get("Kzend_Logger")->info('Can Not Send Notification Email ');
        return true;
    }
}
